package com.liesheng.haylou.utils.sp;

import android.content.Context;
import android.content.SharedPreferences;

import com.liesheng.haylou.app.HyApplication;
import com.liesheng.haylou.bean.UserUpdateBean;

public class SpUtil {
    private static final String LOGIN_TOKEN = "login_token";
    private static final String LOGIN_NICK = "login_nick";
    private static final String LOGIN_AVATAR = "login_avatar";
    private static final String USER_INFO_FILE = "user_info_file";
    private static SharedPreferences getSharedPreferences() {
        return HyApplication.mApp.getSharedPreferences(SpKey.PREF_FILE_COMMON, Context.MODE_PRIVATE);
    }

    /**/
    public static boolean put(String key, boolean value) {
        return getSharedPreferences().edit().putBoolean(key, value).commit();
    }

    public static boolean put(String key, int value) {
        return getSharedPreferences().edit().putInt(key, value).commit();
    }

    public static boolean put(String key, long value) {
        return getSharedPreferences().edit().putLong(key, value).commit();
    }

    public static boolean put(String key, float value) {
        return getSharedPreferences().edit().putFloat(key, value).commit();
    }

    public static boolean put(String key, String value) {
        return getSharedPreferences().edit().putString(key, value).commit();
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return getSharedPreferences().getBoolean(key, defaultValue);
    }

    public static int getInt(String key, int defaultValue) {
        return getSharedPreferences().getInt(key, defaultValue);
    }

    public static long getLong(String key, long defaultValue) {
        return getSharedPreferences().getLong(key, defaultValue);
    }

    public static float getFloat(String key, float defaultValue) {
        return getSharedPreferences().getFloat(key, defaultValue);
    }

    public static String getString(String key, String defaultValue) {
        return getSharedPreferences().getString(key, defaultValue);
    }

    /**
     * 保存nick
     * @param nick
     */
    public static void saveNickName(String nick) {
        SharedPreferences sp = HyApplication.getContext().getSharedPreferences(LOGIN_NICK ,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("nick", nick);
        editor.apply();
    }
    /**
     * 获取昵称
     * @return
     */
    public static String getNickName() {
        SharedPreferences sp = HyApplication.getContext().getSharedPreferences(LOGIN_NICK ,Context.MODE_PRIVATE);
        return sp.getString("nick","");
    }

    /**
     * 保存nick
     * @param avatar
     */
    public static void saveAvatar(String avatar) {
        SharedPreferences sp = HyApplication.getContext().getSharedPreferences(LOGIN_AVATAR ,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("avatar", avatar);
        editor.apply();
    }
    /**
     * 获取昵称
     * @return
     */
    public static String getAvatar() {
        SharedPreferences sp = HyApplication.getContext().getSharedPreferences(LOGIN_AVATAR ,Context.MODE_PRIVATE);
        return sp.getString("avatar","");
    }

    /**
     * 保存token
     * @param token
     */
    public static void saveToken(String token) {
        SharedPreferences sp = HyApplication.getContext().getSharedPreferences(LOGIN_TOKEN,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("token", token);
        editor.apply();
    }

    /**
     * 获取token
     * @return
     */
    public static String getToken() {
        SharedPreferences sp = HyApplication.getContext().getSharedPreferences(LOGIN_TOKEN,Context.MODE_PRIVATE);
        return sp.getString("token","");
    }
    /**
     * 保存用户登录后的主要信息
     *
     * @param userUpdateBean
     */
    public static void saveUserInfo(UserUpdateBean userUpdateBean) {
        SharedPreferences sharedPreferences = HyApplication.getContext().getSharedPreferences(USER_INFO_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("nick", userUpdateBean.getData().getNickName());
        editor.putString("avatar",userUpdateBean.getData().getAvatar());
        editor.apply();
    }
    public static void getUserInfo(){
        SharedPreferences sp = HyApplication.getContext().getSharedPreferences(USER_INFO_FILE, Context.MODE_PRIVATE);
        sp.getString("nick","");
        sp.getString("avatar","");
        return;
    }
    /**
     * 移除用户主要信息
     */
    public static void removeUserInfo() {
        SharedPreferences sharedPreferences = HyApplication.getContext().getSharedPreferences(USER_INFO_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove("nick");
        editor.putString("avatar", "");
        //社区退出登录
        editor.apply();
    }
}
